<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">

          <a-col :md="6" :sm="8">
            <a-form-item label="订单号">
              <a-input placeholder="请输入订单号" v-model="queryParam.orderNumber"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="订单类型">
              <a-select placeholder="请输入订单类型" v-model="queryParam.orderType">
                <a-select-option v-for="item in orderTypes" :key="item.key" :value="item.key">{{ item.val }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="订单状态">
              <a-select placeholder="请选择订单状态" v-model="queryParam.orderStatus">
                <a-select-option v-for="item in orderStatusSetect" :key="item.key" :value="item.key">{{ item.val }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="8">
              <a-form-item label="抢单分类">
                <a-select placeholder="抢单分类" v-model="queryParam.grabType">
                  <a-select-option v-for="item in grabTypes" :key="item.key" :value="item.key">{{ item.val }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="要求">
                <a-input placeholder="请输入要求" v-model="queryParam.demand"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="费用">
                <a-input placeholder="请输入费用" v-model="queryParam.cost"></a-input>
              </a-form-item>
            </a-col>
            <a-col v-if="showMarkd" :md="6" :sm="8">
              <a-form-item label="子管理结算状态">
                <a-select placeholder="请选择结算状态" v-model="queryParam.managerClose">
                  <a-select-option v-for="item in managerCloses" :key="item.key" :value="item.key">{{ item.val }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="8" >
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a-button type="primary" @click="searchQueryByManager" icon="search">子管理订单</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-alert message="订单默认结算周期为一星期，每周三结算上周一到周日状态为”可申请结算”的订单。“可申请结算“订单金额累计达300元时，可以联系结算管理员提前结算。" banner />
<!--      <a-button @click="handleAdd" type="primary" icon="plus">发布订单</a-button>-->
      <!--      <a-button type="primary" icon="download" @click="handleExportXls">导出</a-button>
            <a-upload name="file" :showUploadList="false" :multiple="false" :action="importExcelUrl" @change="handleImportExcel">
              <a-button type="primary" icon="import">导入</a-button>
            </a-upload>-->
<!--      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>


      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a href="javascript:;" v-if="record.orderStatus==0" @click="handleEditStatus(record)">申请发货</a>
          <a-divider type="vertical" v-if="record.orderStatus==0" />
          <a href="javascript:;" v-if="record.orderStatus==3 && record.settlementStatus!=2"  @click="handleClose(record)">申请结算</a>
          <a-divider type="vertical" v-if="record.orderStatus==3 && record.settlementStatus!=2" />
          <a href="javascript:;" @click="handleDetail(record)">详情</a>
        </span>
        <span slot="closingCost" slot-scope="text, record">
          <div style="width: 180px" class="contText"  >加价:{{record.raiseNumber?record.raiseNumber+'元':'无'}}，结算{{Number(record.raiseNumber)+Number(record.cost)}}元</div>
        </span>
        <span slot="cost" slot-scope="text">
          {{text}}元
        </span>
        <span slot="fieldText" slot-scope="text">
          <div style="width: 200px"  class="contText" :title="text">{{ text }}</div>
        </span>
        <span slot="orderStatus" slot-scope="text,record">
      <a-tag  :color="getColor(record)" style="width: 100%;color: rgba(0, 0, 0, 0.65);height: 38px;font-size: 14px;line-height: 2.5;" >{{text}}</a-tag>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <orderForm-modal ref="modalForm" @ok="modalFormOk"></orderForm-modal>
    <editOrderFormModal ref="EditStatusModalForm" @ok="modalFormOk"></editOrderFormModal>
  </a-card>
</template>

<script>
import EditOrderFormModal from './modules/EditOrderFormModal'
import OrderFormModal from './modules/OrderFormModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction, httpAction} from "@api/manage";
import {Dict}   from '@/api/Dict'
import {mapGetters} from "vuex";

export default {
  name: "OrderFormList",
  mixins:[JeecgListMixin,Dict],
  components: {
    OrderFormModal,
    EditOrderFormModal
  },
  data () {
    return {
      islike:true,
      description: '订单管理页面',
      showMarkd:false,
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key:'rowIndex',
          width:30,
          align:"center",
          customRender:function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        {
          title: '创建时间',
          align:"center",
          dataIndex: 'createTime',
          width: 160
        },
        {
          title: '订单类型',
          align:"center",
          dataIndex: 'orderType_dictText',
        },
        {
          title: '订单号',
          align:"center",
          dataIndex: 'orderNumber',
        },
        {
          title: '要求',
          align:"center",
          dataIndex: 'demand',
          width: 200,
          ellipsis: true,
          scopedSlots: { customRender: 'fieldText' },
        },
        {
          title: '费用',
          align:"center",
          dataIndex: 'cost',
          scopedSlots: { customRender: 'cost' },
          width: 80
        },
        {
          title: '要求完成时间',
          align:"center",
          dataIndex: 'finishTime',
          width: 160
        },
        {
          title: '指派人',
          align:"center",
          dataIndex: 'createByName',
        },
        {
          title: '加价',
          align:"center",
          dataIndex: 'raiseNumber',
          scopedSlots: { customRender: 'cost' },
        },
        /*{
          title: '加价',
          align:"center",
          dataIndex: 'raiseNumber',
          scopedSlots: { customRender: 'closingCost' },
          width: 180,
          ellipsis: true,
        },*/
        {
          title: '接单人',
          align:"center",
          dataIndex: 'takeOrders',
          width: 80
        },
        {
          title: '订单状态',
          align:"center",
          dataIndex: 'orderStatus_dictText',
          scopedSlots: { customRender: 'orderStatus' },
          width: 200
        },
        /*{
          title: '申请结算情况',
          align:"center",
          dataIndex: 'settlementStatus_dictText',
          width: 150
        },*/
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          scopedSlots: { customRender: 'action' },
          width: 160,
        }
      ],
      url: {
        list: "/orderForm/orderForm/list",
        edit: "/orderForm/orderForm/edit",
        delete: "/orderForm/orderForm/delete",
        deleteBatch: "/orderForm/orderForm/deleteBatch",
        exportXlsUrl: "orderForm/orderForm/exportXls",
        importExcelUrl: "orderForm/orderForm/importExcel",
      },
    }
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    }
  },
  created() {
    if(this.userInfo().rolestr=='admin' || this.userInfo().rolestr=='accounts'|| this.userInfo().rolestr=='manager'){
      this.showMarkd=true;
      this.columns.splice(2,0,{
        title: '子管理结算状态',
        align:"center",
        dataIndex: 'managerClose_dictText',
        width: 60
      });
    }
  },
  methods: {
    ...mapGetters(["userInfo"]),
    handleClose(record){
     let content='提交“可申请结算”订单，至结算管理员后台。';
      var that = this;
      this.$confirm({
        title: "申请结算",
        content: content,
        onOk: function () {
          record.settlementStatus=2;
          httpAction(that.url.edit,record,'put').then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.loadData();
            } else {
              that.$message.warning(res.message);
            }
          });
        }
      });
    },
    handleEditStatus(record){
      this.$refs.EditStatusModalForm.edit(record);
      let orderStatusList=[{key:2,val:'已交货，申请发货'}];
      this.$refs.EditStatusModalForm.setOrderStatusList(orderStatusList);
      this.$refs.EditStatusModalForm.title = "修改状态";
      this.$refs.EditStatusModalForm.disableSubmit = false;
    },
    searchQuery(){
      this.islike=true;
      this.loadData(1);
    },
    searchQueryByManager() {
      this.islike=false;
      this.loadData(1);
    },
    searchReset() {
      this.islike=true;
      this.queryParam = {}
      this.loadData(1);
    },
    loadData(arg) {
      if(!this.url.list){
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.getQueryParams();//查询条件
      if(this.islike){
        params.takeOrders="*"+this.$store.getters.userInfo.username+"*";
      }else {
        params.takeOrders=this.$store.getters.userInfo.username;
      }
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
      })
    },
  }
}
</script>
<style lang="less" scoped>
/** Button按钮间距 */
.ant-btn {
  margin-left: 3px
}
.ant-card-body .table-operator{
  margin-bottom: 18px;
}
.ant-table-tbody .ant-table-row td{
  padding-top:15px;
  padding-bottom:15px;
}
.anty-row-operator button{margin: 0 5px}
.ant-btn-danger{background-color: #ffffff}

.ant-modal-cust-warp{height: 100%}
.ant-modal-cust-warp .ant-modal-body{height:calc(100% - 110px) !important;overflow-y: auto}
.ant-modal-cust-warp .ant-modal-content{height:90% !important;overflow-y: hidden}
.contText{
  overflow: hidden;/*内容超出后隐藏*/
  text-overflow: ellipsis;/* 超出内容显示为省略号*/
  white-space: nowrap;/*文本不进行换行*/
}
</style>